using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_ChooseGiftHeroCell : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_ChooseGiftHeroCell
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExRawImage icon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textName { protected set; get; }
            public UnityEngine.GameObject select { protected set; get; }
            public UnityEngine.GameObject tagGetted { protected set; get; }
            public UnityEngine.GameObject cancelSelect { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExRawImage>("icon", out var __tbv0);
                this.icon = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textName", out var __tbv1);
                this.textName = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("select", out var __tbv2);
                this.select = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagGetted", out var __tbv3);
                this.tagGetted = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("cancelSelect", out var __tbv4);
                this.cancelSelect = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private HeroData heroData;
        private int heroNum;
        #endregion fields

        #region properties

        protected UIB_UIN_ChooseGiftHeroCell ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_ChooseGiftHeroCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            this.ClearData();
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds
        public void Show(int heroCid, int itemNum)
        {
            this.heroNum = itemNum;
            this.heroData = HeroModule.Instance.GetHero(heroCid);
            if (this.heroData == null)
            {
                LogGame.LogError($"[{heroCid}] hero is null");
                this.Hide();
                return;
            }
            this.Show();
            this.ShowName();
            this.ShowIcon();
            this.ShowGettedState();
        }

        private void ClearData()
        {
            this.heroData = null;
            this.heroNum = 0;
        }

        private void ShowName()
        {
            this.ui.textName.text = this.heroData.cfgData.Name;
        }

        private void ShowIcon()
        {
            this.SetRawImageTexture(this.ui.icon, this.heroData.cfgSkin.HeroHalfBodyImage_fullPath);
        }

        private void ShowGettedState()
        {
            this.ui.tagGetted.SetActive(this.heroData.isGeted);
        }

        public void SetSelected(bool isSelected, bool isSingle)
        {
            this.ShowSelected(isSelected);
            bool isActiveBtn = (isSelected && !isSingle);
            this.ShowCancelSelectedBtn(isActiveBtn);
        }

        private void ShowSelected(bool isSelected)
        {
            this.ui.select.SetActive(isSelected);
        }

        private void ShowCancelSelectedBtn(bool isSelected)
        {
            this.ui.cancelSelect.SetActive(isSelected);
        }
        #endregion methonds
    }
}
